<?php

namespace app\api\Logic\Broadcast;


use app\api\model\BroadcastModel;
use app\api\model\ManystoreShopModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class BroadcastLogic
{
    /**
     * 获取直播信息详情
     * @param $shopId
     * @param $broadcastId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getInfo($shopId,$broadcastId){
        //直播信息详情
        $info = BroadcastModel::where('shop_id',$shopId)
            ->where('id',$broadcastId)
            ->field('id,shop_id,title,image,type,price,teacher_id,assistant_id,view_type,share_type,start_time,end_time,bg_image,time_length,live_status,content')
            ->find();
        $info = objToArray($info) ?? [];

        if(!empty($info)){
            //校区/平台信息
            $shopInfo = [];
            //教师信息
            $teacherInfo = [];
            if($info['shop_id'] == 0){
                //平台创建，显示平台名称和logo
                $shopInfo['name'] = db('config')->where('name','name')
                    ->where('group','basic')
                    ->value('value');
                $shopInfo['logo'] = db('config')->where('name','logo')
                    ->where('group','basic')
                    ->value('value');

                //教师信息
                $teacherInfo = db('admin')->where('id',$info['teacher_id'])
                    ->field('id,username,nickname,avatar')
                    ->find();
            }else if($info['shop_id'] > 0){
                //校区创建
                $shopInfo = ManystoreShopModel::where('id',$info['shop_id'])
                    ->field('name,logo')
                    ->find();

                //教师信息
                $teacherInfo = db('manystore')->where('id',$info['teacher_id'])
                    ->where('shop_id',$info['shop_id'])
                    ->field('id,username,nickname,avatar')
                    ->find();
            }
            $info['shop_info'] = $shopInfo;
            $info['teacher_info'] = $teacherInfo;
        }

        return $info;
    }

    /**
     * 判断直播课程是否存在
     * @param $shopId
     * @param $broadcastId
     * @return bool
     * @throws Exception
     */
    public function exists($shopId,$broadcastId){
        $count = BroadcastModel::where('shop_id',$shopId)
            ->where('id',$broadcastId)
            ->count();
        if($count){
            return true;
        }
        return false;
    }
}
